<template>
  <div>
    <!-- 头部 -->
    <div class="header">
      <div class="top-bar">
        <div class="back">
          <a href="javascript:history.go(-1);">
            <i class="fa fa-angle-left fa-lg" style="color: black"></i>
          </a>
        </div>
        <div class="title">设置密码</div>
        <div class="register"></div>
      </div>
    </div>
    <div class="gap"></div>
    <!-- input框 -->
    <van-form>
        <van-field v-model="password" name="password" center clearable label="密码" placeholder="请输入密码" 
        :rules="[
          {
            validator: rulesPassword,
            trigger: 'onBlur',
          },
        ]">
        </van-field>
        <van-field v-model="passwordAgain" name="password" center clearable label="确认密码" placeholder="再次确认密码" 
                :rules="[
                    {
                      validator: rulesPasswordAgain,
                      trigger: 'onBlur',
                    },
                  ]">
        </van-field>
     </van-form>
      <!-- 登录按钮 -->
      <div style="margin: 16px;">
          <van-button type="warning info"  block native-type="submit" @click="onLogin">确认</van-button>
      </div>
  </div>
</template>

<script>
import { Toast } from 'vant';
export default {
  name: "LookFor",
  data() {
    return {
        password: "",
        passwordAgain:""
    };
  },
  methods: {
     rulesPassword(val, rule) {
      if (!val) {
        // rule.message = '请输入密码';
        return false;
      }
      if (val.length < 4) {
        rule.message = '密码长度不能小于4位';
        return false;
      }
      if (val.length > 20) {
        rule.message = '密码长度不能大于20位';
        return false;
      }
      return true;
    },
    /**
     * @description: 校验确认密码
     */
    rulesPasswordAgain(val, rule) {
      if (!val) {
        // rule.message = '请输入密码';
        return false;
      }
      if (val.length < 4) {
        rule.message = '密码长度不能小于4位';
        return false;
      }
      if (val.length > 20) {
        rule.message = '密码长度不能大于20位';
        return false;
      }
      return true;
    },
    async onLogin(){
        const { password,passwordAgain} = this;
        if(password !== passwordAgain) {
            this.$toast('密码与上次不一致');
        } else {
            Toast.success('设置成功');
            setTimeout(() => {
                Toast.clear();
                this.$router.push({
                    path:'/login'
                })
            },1000)
        }
    }
  } 
}

</script>

<style scoped lang="less">
.header {
  text-align: center;
  background: #fcfcfc;
  border-bottom: 1px solid #f63;
  padding: 0 15px;
  .top-bar {
    display: -webkit-inline-box;
    -webkit-box-pack: justify;
    -webkit-box-align: center;
    line-height: 45px;
    width: 100%;
    .back {
      display: block;
      -webkit-box-flex: 1;
      text-align: left;
      a {
        display: block;
        width: 100%;
        height: 45px;
        position: relative;
        text-decoration: none;
      }
    }
    .title {
      font-size: 18px;
      -webkit-box-flex: 3;
      text-align: center;
    }
    .register {
      -webkit-box-flex: 1;
    }
  }
}
van-field--error .van-field__control,
.van-field--error .van-field__control::placeholder {
  color: #666;
}
.gap {
  height: 10px;
}
.form-info {
  line-height: 12px;
  margin-top: 5px;
  text-align: center;
  color: #999;
}



</style>